/** Author: David Powell
This file produces Figure A14
**/


clear all
set more off

global dir "/jules/b/dpowell"
global DATA "${dir}/purdue/replication/DATA"
global OUTPUT "${dir}/purdue/replication/output"



use ${DATA}/finaldata if year<2006

**construct outcomes and annualize them
gen mc_oxycontin=1000*oxycontin_SDUD/medicaidb
replace mc_oxycontin=mc_oxycontin*4/numobs

gen mc_oxycodone=1000*oxycodone_SDUD/medicaidb
replace mc_oxycodone=mc_oxycodone*4/numobs

set scheme s2color  




***REPEAL DATES***
gen p1=.
replace p1=2004 if stf==6
replace p1=1997 if stf==16
replace p1=2000 if stf==17
replace p1=2001 if stf==36
replace p1=1999 if stf==48
replace p1=1994 if stf==18 | stf==26
replace p1=99 if p1==.



***EVENT STUDY****
gen relyear=year-p1
replace relyear=. if p1==99

replace relyear=-3 if relyear<-3
replace relyear=5 if relyear>5 & relyear<.

tab relyear, gen(aa) nof
tab stf, gen(ss) nof
tab year, gen(yy) nof
drop aa3
for var aa*: replace X=0 if X==.

for num 1/2: gen betaX=0
for num 1/2: gen lowX=0
for num 1/2: gen highX=0


reg mc_oxycontin ss* yy* aa* [aw=medicaidben] if year>=1996 & p1!=1994 , cluster(stf)
foreach yyy of numlist 1/2 4/9 {
	replace beta1=_b[aa`yyy'] if relyear==`yyy'-4
	boottest aa`yyy', boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low1=A[1,1] if relyear==`yyy'-4
	replace high1=A[1,2] if relyear==`yyy'-4
	if (`yyy'==4) {
		replace high1=A[2,2] if relyear==`yyy'-4
	}
}

reg mc_oxycodone ss* yy* aa* [aw=medicaidb] if p1!=1994, cluster(stf)
foreach yyy of numlist 1/2 4/9 {
	replace beta2=_b[aa`yyy'] if relyear==`yyy'-4
	boottest aa`yyy', boottype(wild) weighttype(webb) reps(9999) seed(23857389)
	matrix A=r(CI)
	replace low2=A[1,1] if relyear==`yyy'-4
	replace high2=A[1,2] if relyear==`yyy'-4
}



preserve
bys relyear: keep if _n==1
keep if rely<.


# delimit ;


twoway (rarea low1 high1 relyear  , color(gs14)) (connected beta1 relyear, yaxis(1)  sort lcolor(black) mcolor(black)) 
  , yline(0, lpattern(dash) lcolor(black))   legend(label(3 "Estimate") label(1 "95% Confidence Interval")  order(3 1) rows(1)) graphregion(color(white))
xlabel(-3 "-3+" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5+", angle(45)) xline(0, lcolor(black)) ylabel(,nogrid) xtitle("Year Relative to Triplicate Repeal") ytitle("Coefficient Estimate", axis(1)) ;
gr export ${OUTPUT}/figA14a.eps, replace;
gr export figA14a.pdf, replace;



twoway (rarea low2 high2 relyear , color(gs14))  (connected beta2 relyear, yaxis(1)  sort lcolor(black)  mcolor(black)) 
  , yline(0, lpattern(dash) lcolor(black))   legend(label(2 "Estimate") label(1 "95% Confidence Interval")  order(2 1) rows(1)) graphregion(color(white))
xlabel(-3 "-3+" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5+", angle(45)) xline(0, lcolor(black)) ylabel(,nogrid) xtitle("Year Relative to Triplicate Repeal") ytitle("Coefficient Estimate", axis(1)) ;
gr export ${OUTPUT}/figA14b.eps, replace;
gr export figA14b.pdf, replace;

# delimit cr

restore



